package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.ResponseData;
import com.qfedu.entity.Staff;
import com.qfedu.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class TStaffController {
    @Autowired
    private StaffService staffService;


    @RequestMapping("/staffAdd.do")
    public ResponseData StaffAdd(Staff staff, HttpServletRequest request) {
        ResponseData responseData = new ResponseData();

        int num = staffService.staffAdd(staff);

        if (num > 0) {
            responseData.setMsg("success");
            responseData.setCode(0);// 0代表成功
        } else {
            responseData.setMsg("fail");
            responseData.setCode(1);// 1 代表失败
        }
        return responseData;
    }

    @RequestMapping("/staffAll.do")
    public ResponseData<List<Staff>> findAllDepart(int page, int limit) {

        // 开启分页
        PageHelper.startPage(page, limit);

        // 按照分页查询
        List<Staff> staffList = staffService.findAll();

        PageInfo<Staff> staffPageInfo = new PageInfo<>(staffList);

        // 获取数据表中所有数据行数
        int count = (int) staffPageInfo.getTotal();

        ResponseData<List<Staff>> responseData = new ResponseData<>();

        // 成功
        responseData.setCode(0);
        responseData.setMsg("success");
        responseData.setCount(count);
        responseData.setData(staffList);

        return responseData;
    }

    @RequestMapping("/staffdelete.do")
    public ResponseData delete(int id) {
        staffService.delete(id);

        ResponseData responseData = new ResponseData();

        responseData.setCode(0);
        responseData.setMsg("success");

        return responseData;

    }
}
